package com.aishare.qicaitaoke.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.util.Set;

/**
 * 作者luwenming
 * 时间2018/1/31
 */

public class SPUtils {

    private SharedPreferences sp;
    private static SPUtils spUtils;

    private SPUtils(Context context, String spName) {
        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
    }

    public static SPUtils getInstance(Context context, String spName) {
        if(TextUtils.isEmpty(spName)) {
            spName = "spUtils";
        }
        if (spUtils == null) {
            spUtils = new SPUtils(context, spName);
        }
        return spUtils;
    }

    /**
     *
     * @param key
     * @param value
     */
    public void put(String key, String value) {
        sp.edit().putString(key, value).apply();
    }

    public void put(String key,int value){
        sp.edit().putInt(key,value).apply();
    }

    public void  put(String key,long value){
        sp.edit().putLong(key,value).apply();
    }

    public void put(String key,float value){
        sp.edit().putFloat(key,value).apply();
    }
    public void put(String key, Set<String> value){
        sp.edit().putStringSet(key,value).apply();
    }

    public Set<String> getStringSet(String key,Set<String> defaultValue){
        return sp.getStringSet(key,defaultValue);
    }

    public float getFloat(String key,float defaultValue){
         return sp.getFloat(key,defaultValue);
    }
    public long getLong(String key , long defaultValue){
        return sp.getLong(key,defaultValue);
    }

    public int getInt(String key,int defaultValue){
        return sp.getInt(key,defaultValue);
    }

    public String getString(String key,String defaultValue){
        return sp.getString(key,defaultValue);
    }

    public void remove(String key){
        sp.edit().remove(key).apply();
    }

    public boolean contains(String key){
        return  sp.contains(key);
    }



}
